package Action;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import sun.io.Converters;

import Aptech.Common.Formater;
import Aptech.Service.SV_Buitls;
import Aptech.Service.SV_Contract;
import Aptech.Service.SV_Custommers;
import Aptech.Service.SV_Rooms;
import Form.BuiltsForm;
import Form.ContractForm;
import HibernateMappingPoJo.Builts;
import HibernateMappingPoJo.PurchaseContract;
import HibernateMappingPoJo.Rooms;




public class ContractAction extends DispatchAction{
	private ContractForm contactForm;
	private SV_Contract mgContact = new SV_Contract();
	private SV_Custommers mgCus = new SV_Custommers();
	private SV_Rooms mgRoom = new SV_Rooms();
	private SV_Buitls mgBuilt = new SV_Buitls();
	
    public ActionForward getAllContract(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {       
    	contactForm = (ContractForm) form;
    	contactForm.setArrContact(mgContact.getAllContract());
    	request.setAttribute("sitemap", "List of Purchase Contract");
    	
        return mapping.findForward("list");
    }
    
    public ActionForward editContract(ActionMapping map, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
    	contactForm = (ContractForm) form;
    	String id = request.getParameter("id");
    	request.setAttribute("arrPaymentMode",mgContact.getAllPaymentMode());
    	request.setAttribute("sitemap", "Edit A Purchase Contract");
    	
    	String type = request.getParameter("type");
    	if (type.equals("edit1")) {
            request.setAttribute("id", id);
            request.setAttribute("type", type);
            
            // Set value for Form
            PurchaseContract contact = mgContact.getContractByID(id);
            Rooms room = mgRoom.getRoomByID(contact.getIdRoom());
            contactForm.setName(contact.getName());
            contactForm.setContractNumber(contact.getContractNumber());
            contactForm.setDateCreate(Formater.date2str(contact.getDateCreate()));
            contactForm.setId(contact.getId());
            contactForm.setNote(contact.getNote());
            contactForm.setNameCustommers(mgCus.getCustommerByID(contact.getIdCustommers()).getName());
            contactForm.setIdCustommers(contact.getIdCustommers());
            contactForm.setIdPaymentMode(contact.getPaymentMode());
            contactForm.setIdRoom(room.getId());
            contactForm.setNameRooms(room.getName());
            contactForm.setBuilt(mgBuilt.getBuiltByID(room.getIdBuilt()).getName());
            contactForm.setContent(contact.getContent());
            contactForm.setTotalPrice(contact.getTotalPrice().toString());
        } else {       	
        	PurchaseContract contact = new PurchaseContract();           
        	contact.setName(contactForm.getName());
        	contact.setId(contactForm.getId());
        	contact.setIdCustommers(contactForm.getIdCustommers());
        	contact.setIdRoom(contactForm.getIdRoom());
        	contact.setNote(contactForm.getNote());
        	contact.setPaymentMode(contactForm.getIdPaymentMode());
        	contact.setContractNumber(contactForm.getContractNumber());
        	contact.setDateCreate(Formater.str2SQLdate(contactForm.getDateCreate()));
        	contact.setContent(contactForm.getContent());
        	contact.setTotalPrice(Double.valueOf(contactForm.getTotalPrice()));
        	mgContact.editContract(contact);
        	request.setAttribute("save", "save");
        }
        return map.findForward("edit");
    }
    
    public ActionForward addContract(ActionMapping map, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
    	
    	contactForm = (ContractForm) form;
    	request.setAttribute("arrPaymentMode",mgContact.getAllPaymentMode());
    	request.setAttribute("sitemap", "Add A New Purchase Contract");
    	String type = request.getParameter("type");
        if (type == null) {
            request.setAttribute("type", "add");
            return map.findForward("edit");
        } else {
            UUID id = UUID.randomUUID();
            
            PurchaseContract contact = new PurchaseContract();           
        	contact.setName(contactForm.getName());
        	contact.setId(id.toString());
        	contact.setIdCustommers(contactForm.getIdCustommers());
        	contact.setIdRoom(contactForm.getIdRoom());
        	contact.setNote(contactForm.getNote());
        	contact.setPaymentMode(contactForm.getIdPaymentMode());
        	contact.setContractNumber(contactForm.getContractNumber());
        	contact.setDateCreate(Formater.str2SQLdate(contactForm.getDateCreate()));
        	contact.setContent(contactForm.getContent());
        	contact.setTotalPrice(Double.valueOf(contactForm.getTotalPrice()));
        	mgContact.addContract(contact);
        	request.setAttribute("save", "save");
        }

        return map.findForward("edit");
    }
    
    public ActionForward deleteContract(ActionMapping map, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
    	
    	String id = request.getParameter("id");
    	mgContact.deleteContract(id);
        response.sendRedirect("/NTB/contract.do?method=getAllContract");
        
    	return map.findForward("list");
    }

    public ActionForward detailContract(ActionMapping map, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
    	
    	contactForm = (ContractForm) form;
        String id = request.getParameter("id");
        request.setAttribute("id", id);
        request.setAttribute("type", "detail");
        request.setAttribute("arrPaymentMode",mgContact.getAllPaymentMode());
        request.setAttribute("sitemap", "Detail of A New Purchase Contract");
            // Set value for Formpe
        PurchaseContract contact = mgContact.getContractByID(id);
        Rooms room = mgRoom.getRoomByID(contact.getIdRoom());
        contactForm.setName(contact.getName());
        contactForm.setContractNumber(contact.getContractNumber());
        contactForm.setDateCreate(Formater.date2str(contact.getDateCreate()));
        contactForm.setId(contact.getId());
        contactForm.setNote(contact.getNote());
        contactForm.setNameCustommers(mgCus.getCustommerByID(contact.getIdCustommers()).getName());
        contactForm.setIdCustommers(contact.getIdCustommers());
        contactForm.setIdPaymentMode(contact.getPaymentMode());
        contactForm.setIdRoom(room.getId());
        contactForm.setNameRooms(room.getName());
        contactForm.setBuilt(mgBuilt.getBuiltByID(room.getIdBuilt()).getName());
        contactForm.setContent(contact.getContent());
        contactForm.setTotalPrice(contact.getTotalPrice().toString());
    	return map.findForward("edit");
    }
    
    public ActionForward findContract(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {       
    	contactForm = (ContractForm) form;
    	contactForm.setArrContact(mgContact.getAllContract());

        return mapping.findForward("findContract");
    }
    
    public ActionForward searchForName(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {  
    	String key = request.getParameter("key");
    	contactForm = (ContractForm) form;
    	if(mgContact.searchForName(key) != null){
    		contactForm.setArrContact(mgContact.searchForName(key));
    	}
    	//contactForm.setArrContact(mgContact.getAllContract());
    	request.setAttribute("sitemap", "List of Purchase Contract");
    	
    	 return mapping.findForward("list");
    }

}
